﻿using System;
using System.Windows.Forms;
using WorkLogger.Client.Controls;
using WorkLogger.Client.Properties;

namespace WorkLogger.Client.Forms {
	public partial class MainForm : Form, IMainForm {
		public TimeSpan WorkDayDuration { get; set; }

		public MainForm() {
			InitializeComponent();
		}

		public event EventHandler Tick;
		public event EventHandler StartWorkingChanged;

		private DateTime currentDate;

		public DateTime CurrentDate {
			get { return currentDate; }
			set {
				currentDateLabel.Text = string.Format("Текущая дата: {0}", value.ToString("dd.MM.yyy"));
				currentDate = value;
				UpdateDayOfWeekImage();
			}
		}

		private void UpdateDayOfWeekImage() {
			switch (CurrentDate.DayOfWeek) {
				case DayOfWeek.Monday:
					dayOfWeekPictureBox.Image = Resources.Monday;
					toolTip.SetToolTip(dayOfWeekPictureBox, "Понедельник");
					break;
				case DayOfWeek.Tuesday:
					dayOfWeekPictureBox.Image = Resources.Tuesday;
					toolTip.SetToolTip(dayOfWeekPictureBox, "Вторник");
					break;
				case DayOfWeek.Wednesday:
					dayOfWeekPictureBox.Image = Resources.Wednesday;
					toolTip.SetToolTip(dayOfWeekPictureBox, "Среда");
					break;
				case DayOfWeek.Thursday:
					dayOfWeekPictureBox.Image = Resources.Thursday;
					toolTip.SetToolTip(dayOfWeekPictureBox, "Четверг");
					break;
				case DayOfWeek.Friday:
					dayOfWeekPictureBox.Image = Resources.Friday;
					toolTip.SetToolTip(dayOfWeekPictureBox, "Пятница");
					break;
				case DayOfWeek.Saturday:
					dayOfWeekPictureBox.Image = Resources.Holiday;
					toolTip.SetToolTip(dayOfWeekPictureBox, "Суббота");
					break;
				case DayOfWeek.Sunday:
					dayOfWeekPictureBox.Image = Resources.Holiday;
					toolTip.SetToolTip(dayOfWeekPictureBox, "Воскресенье");
					break;
			}
		}

		public DateTime StartWorking {
			get { return startWorkingDateTimePicker.Value; }
			set { startWorkingDateTimePicker.Value = value; }
		}

		public void SetWorkedTime(TimeSpan workedTime, TimeSpan workDayDuration) {
			workedTimeLabel.Text = string.Format("Отработано за день: {0}", TimeSpanFormatHelper.FormatDuration(workedTime));
			workRemainderLabel.Text = workedTime > workDayDuration
			                          	? string.Format("Переработка: {0}",
			                          	                TimeSpanFormatHelper.FormatDuration(workedTime - workDayDuration))
			                          	: string.Format("Осталось пахать: {0}",
			                          	                TimeSpanFormatHelper.FormatDuration(workDayDuration - workedTime));
		}

		public ILunchTimeView LunchTimeView {
			get { return lunchTimeView; }
		}

		public IMainMenuView MainMenu {
			get { return mainMenuView; }
		}

		private void TimerTick(object sender, EventArgs e) {
			if (Tick != null)
				Tick(this, e);
		}

		private void StartWorkingDateTimePickerValueChanged(object sender, EventArgs e) {
			if (StartWorkingChanged != null) {
				StartWorkingChanged(this, e);
			}
		}

		private void MainFormFormClosing(object sender, FormClosingEventArgs e) {
			if (e.CloseReason == CloseReason.UserClosing) {
				e.Cancel = true;
				WindowState = FormWindowState.Minimized;
			}
		}
	}
}